package com.itstudio.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itstudio.portal.entity.Industry;
import com.itstudio.portal.entity.RestResponse;
import com.itstudio.portal.entity.dto.IndusDetailDto;
import com.itstudio.portal.enums.HttpCodeEnum;
import com.itstudio.portal.mapper.IndustryMapper;
import com.itstudio.portal.service.IndustryService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Stack;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2024/11/15
 */
@Service("IndustryService")
public class IndustryServiceImpl implements IndustryService {
    @Resource
    private IndustryMapper industryMapper;

    @Override
    public RestResponse getIndustryTree(String parentId) {
        LambdaQueryWrapper<Industry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Industry::getParentId, parentId);
        List<Industry> industries = industryMapper.selectList(queryWrapper);
        if (industries == null || industries.isEmpty()) {
            String id = parentId;
            Stack<String> fullIndustryStack = new Stack<>();
            StringBuilder fullIndustry = new StringBuilder();
            while (!"0".equals(id)) {

                Industry industry = industryMapper.selectById(id);
                if (industry == null) {
                    return RestResponse.failure(HttpCodeEnum.UNKNOWN_ERROR);
                }
                id = industry.getParentId();
                fullIndustryStack.push(industry.getCategoryName());
            }
            while (!fullIndustryStack.isEmpty()) {
                fullIndustry.append(fullIndustryStack.pop()+">");
            }
            // 去掉最后一个 ">"
            if (fullIndustry.length() > 0) {
                fullIndustry.setLength(fullIndustry.length() - 1);
            }
            return RestResponse.success(fullIndustry.toString());

        }


        return RestResponse.success(industries);
    }

    @Override
    public RestResponse getIndustriesForName(String name){
        LambdaQueryWrapper<Industry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Industry::getCategoryName,name);
        List<Industry> industries = industryMapper.selectList(queryWrapper);
        return RestResponse.success(industries);
    }

    @Override
    public RestResponse getIndustryName(IndusDetailDto indicatorDetailDto) {

        //如果前台传递的参数为null则直接返回空
        if(!StringUtils.hasText(indicatorDetailDto.getIndus())){

            indicatorDetailDto.setIndus("");
            return RestResponse.success(indicatorDetailDto);

        }

        //获取前台传递过来的参数
        String indus = indicatorDetailDto.getIndus();
        //进行分隔
        if(Objects.nonNull(indus)){
            String[] split = indus.split(",");
            Industry industry = industryMapper.selectById(split[0]);
            Industry industry1 = industryMapper.selectById(split[1]);
            indicatorDetailDto.setIndus(industry.getCategoryName()+"/"+industry1.getCategoryName());
        }


        return RestResponse.success(indicatorDetailDto);
    }

}
